//
//  WKAddressViewController.m
//  ourToken
//
//  Created by wakeup on 2018/5/31.
//  Copyright © 2018年 ourtoken. All rights reserved.
//

#import "WKAddressViewController.h"
#import "WKBaseHeader.h"

@interface WKAddressViewController ()<UITableViewDelegate, UITableViewDataSource, WKAddressTableViewCellDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *addAddressView;//添加地址view
@property (nonatomic, strong) UIButton *addAddressBtn;//添加地址
@property (nonatomic, strong) NSMutableArray *addressArray;//数据源

@end

@implementation WKAddressViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavigationView];
    [self layoutUI];
    self.view.backgroundColor = [UIColor whiteColor];
    [self getAddressListNetworking];
}

- (void)setNavigationView {
    [super setNavigationView];
    self.navigationView.topTitleLabel.text = @"地址管理";
    [self.tableView registerNib:[UINib nibWithNibName:@"WKAddressTableViewCell" bundle:nil] forCellReuseIdentifier:@"AddressCell"];
}

- (void)layoutUI {
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.addAddressView];
    [self.addAddressView addSubview:self.addAddressBtn];
}

//获取地址列表
- (void)getAddressListNetworking {
    [httpManager getAddressListWithPageIndex:1 pageSize:10 success:^(NSDictionary *result) {
        
        NSDictionary *data = [result objectForKey:@"data"];
        for (NSDictionary *dict in data) {
            WKAddressListModel *list = [WKAddressListModel mj_objectWithKeyValues:dict];
            [self.addressArray addObject:list];
        }
        DLog(@"%@",result);
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        
    }];
}

#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.addressArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 150;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WKAddressTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AddressCell" forIndexPath:indexPath];
    cell.delegate = self;
    WKAddressListModel *addressListModel = self.addressArray[indexPath.row];
    cell.addressListModel = addressListModel;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - WKAddressTableViewCellDelegate
- (void)updateAddressWithTableViewCell:(WKAddressTableViewCell *)cell andUpdateBtn:(UIButton *)button {
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    WKAddAddressViewController *addAddress = [[WKAddAddressViewController alloc] init];
    addAddress.status = 1;
    addAddress.addressListModel = self.addressArray[indexPath.row];
    [self.navigationController pushViewController:addAddress animated:YES];
}

- (void)deleteAddressWithTableViewCell:(WKAddressTableViewCell *)cell andDeleteBtn:(UIButton *)button {
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    [httpManager deleteAddressWithAddressId:indexPath.row success:^(NSDictionary *result) {
        
    } failure:^(NSError *error) {
        
    }];
}

#pragma mark - 添加地址
- (void)clickAddAddressBtn:(UIButton *)sender {
    WKAddAddressViewController *addAddress = [[WKAddAddressViewController alloc] init];
    addAddress.status = 0;
    [self.navigationController pushViewController:addAddress animated:YES];
}

#pragma mark - 懒加载
- (UITableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, AppManager.navigationHeight, AppManager.width, AppManager.height-AppManager.navigationHeight-AppManager.tabBarHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, AppManager.width, 20)];
        _tableView.backgroundColor = AppManager.tableBackColor;
    }
    return _tableView;
}

- (UIView *)addAddressView {
    if (_addAddressView == nil) {
        _addAddressView = [[UIView alloc] initWithFrame:CGRectMake(0, AppManager.height-AppManager.tabBarHeight, AppManager.width, AppManager.tabBarHeight)];
        _addAddressView.backgroundColor = [UIColor whiteColor];
    }
    return _addAddressView;
}

- (UIButton *)addAddressBtn {
    if (_addAddressBtn == nil) {
        _addAddressBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        _addAddressBtn.frame = CGRectMake(0, 0, AppManager.width, AppManager.tabBarHeight);
        [_addAddressBtn setTitle:@"添加" forState:UIControlStateNormal];
        [_addAddressBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _addAddressBtn.backgroundColor = AppManager.themeColor;
        _addAddressBtn.titleLabel.font = AppManager.btnFont;
        [_addAddressBtn addTarget:self action:@selector(clickAddAddressBtn:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addAddressBtn;
}

- (NSMutableArray *)addressArray {
    if (_addressArray == nil) {
        _addressArray = [[NSMutableArray alloc]init];
    }
    return _addressArray;
}

@end
